using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

using FreeFlow.Administration;

namespace FreeFlowAdministrator
{
	/// <summary>
	/// Summary description for LibrariesControl.
	/// </summary>
	public class LibrariesControl : AdminUserControl
	{
    private ExtendedDataGrid librariesGrid;
    private FreeFlowAdministrator.UIActionsControl uiActionsControl;
    private IContainer components;

		public LibrariesControl()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

      UIActionList actions = new UIActionList();
      actions.Add("Refresh", "Refresh the list", new ActionExecuteDelegate(RefreshList));
      actions.Add("Copy to clipboard", "Copy the list of libraries to the clipboard", 
        new ActionExecuteDelegate(CopyToClipboard));
      uiActionsControl.Actions = actions;
		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
      this.components = new System.ComponentModel.Container();
      this.librariesGrid = new FreeFlowAdministrator.ExtendedDataGrid();
      this.uiActionsControl = new FreeFlowAdministrator.UIActionsControl();
      ((System.ComponentModel.ISupportInitialize)(this.librariesGrid)).BeginInit();
      this.SuspendLayout();
      // 
      // librariesGrid
      // 
      this.librariesGrid.AllowNavigation = false;
      this.librariesGrid.CaptionVisible = false;
      this.librariesGrid.DataMember = "";
      this.librariesGrid.Dock = System.Windows.Forms.DockStyle.Fill;
      this.librariesGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
      this.librariesGrid.Location = new System.Drawing.Point(0, 0);
      this.librariesGrid.Name = "librariesGrid";
      this.librariesGrid.Size = new System.Drawing.Size(362, 312);
      this.librariesGrid.TabIndex = 0;
      // 
      // uiActionsControl
      // 
      this.uiActionsControl.Actions = null;
      this.uiActionsControl.Dock = System.Windows.Forms.DockStyle.Right;
      this.uiActionsControl.Location = new System.Drawing.Point(362, 0);
      this.uiActionsControl.Name = "uiActionsControl";
      this.uiActionsControl.Size = new System.Drawing.Size(150, 312);
      this.uiActionsControl.TabIndex = 1;
      // 
      // LibrariesControl
      // 
      this.Controls.Add(this.librariesGrid);
      this.Controls.Add(this.uiActionsControl);
      this.Name = "LibrariesControl";
      this.Size = new System.Drawing.Size(512, 312);
      ((System.ComponentModel.ISupportInitialize)(this.librariesGrid)).EndInit();
      this.ResumeLayout(false);

    }
		#endregion

    private LibraryCollection libraries;
    public void LoadLibraries(LibraryCollection libraries)
    {
      this.libraries = libraries;
      librariesGrid.DataSource = null;
      librariesGrid.DataSource = libraries;
      librariesGrid.ClearColumns();
      librariesGrid.AddColumn("Name", "Name");
      librariesGrid.AddColumn("LastPublished", "Last published");
      if (libraries.Server.SchemaVersion >= SchemaVersion.Version70)
        librariesGrid.AddColumn("ProcedureCount", "Number of procedures");
      librariesGrid.AutosizeColumns();
    }

    private string RefreshList()
    {
      libraries.Refresh();
      LoadLibraries(libraries);
      OnRefreshRequired();
      return null;
    }

    private string CopyToClipboard()
    {
      librariesGrid.CopyToClipboard();
      return null;
    }
	}
}
